package com.yang.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yang.domain.ChatFriendRel;
import com.yang.domain.User;
import com.yang.handler.UserThreadLocal;
import com.yang.service.ChatFriendRelService;
import com.yang.service.UserService;
import com.yang.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 不是菜鸡爱编程
 * @Date: 2024/03/17/11:17
 * @Description:
 */
@RestController
@RequestMapping("chat_friend_rel")
public class ChatFriendRelController {
    @Autowired
    private ChatFriendRelService chatFriendRelService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public R list() {
        User user = UserThreadLocal.get();
        List<ChatFriendRel> list = chatFriendRelService.list(new LambdaQueryWrapper<ChatFriendRel>()
                .select(ChatFriendRel::getFriendId)
                .eq(ChatFriendRel::getUserId, user.getId()));
        List<Integer> userIdList = list.stream().distinct().map(ChatFriendRel::getFriendId).collect(Collectors.toList());
        List<User> userList = userService.list(new LambdaQueryWrapper<User>().in(User::getId, userIdList, CollUtil.isNotEmpty(userIdList)));
        return R.ok().setData(userList);
    }
}
